package com.maistars.spider.common.util;

import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author dhr
 * @date 2020/5/30 4:35 下午
 */
public class HtmlUtil {

    public static List<String> specialCharFilte(List<String> list){
        return list.stream().filter(s -> !"`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ ".contains(s)).collect(Collectors.toList());
    }

    public static String htmlReplace(String str){
        if(str==null||str.length()==0){
            return "";
        }
        str = str.replace("&ldquo;", "“");
        str = str.replace("&rdquo;", "”");
        str = str.replace("&nbsp;", " ");
        str = str.replace("&", "&amp;");
        str = str.replace("&#39;", "'");
        str = str.replace("&rsquo;", "’");
        str = str.replace("&mdash;", "—");
        str = str.replace("&ndash;", "–");
        str = str.replace("<img", "");
        str = str.replace("img>", "");
        str = str.replace("\"", "'");
        return str;
    }

    public static String getUrlProtocol(String url){
        if(url==null||url.length()==0){
            return "";
        }
        return url.split(":")[0];
    }

    public static String getDomainName(String url) {
        String host = "";
        try {
            URL Url = new URL(url);
            host = Url.getHost();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return host;
    }

    public static String getDomainNameAndProtocol(String url) {
        String host = "";
        try {
            URL Url = new URL(url);
            host = Url.getHost();
            host = Url.getProtocol()+"://"+host;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return host;
    }

    public static String handleLinkSingleAndDoubleSlash(String doc, String url){
        return handleLinkSingleSlash(handleLinkDoubleSlash(doc,url),url);
    }

    /**
     * 处理双斜杠
     */
    public static String handleLinkDoubleSlash(String doc, String url){
        if(url.startsWith("http:")){
            doc = doc.replace("src=\"//","src=\"http://");
            doc = doc.replace("src='//","src='http://");
            doc = doc.replace("href=\"//","href=\"http://");
            doc = doc.replace("href='//","href='http://");

        }else if(url.startsWith("https:")){
            doc = doc.replace("src=\"//","src=\"https://");
            doc = doc.replace("src='//","src='https://");
            doc = doc.replace("href=\"//","href=\"https://");
            doc = doc.replace("href='//","href='https://");
        }
        return doc;
    }

    /**
     * 处理单斜杠
     */
    public static String handleLinkSingleSlash(String doc, String url){
        String domainName = HtmlUtil.getDomainNameAndProtocol(url)+"/";
        if(domainName.startsWith("http:")){
            doc = doc.replace("src=\"/","src=\""+domainName);
            doc = doc.replace("src='/","src='"+domainName);
            doc = doc.replace("href=\"/","href=\""+domainName);
            doc = doc.replace("href='/","href='"+domainName);

        }else if(domainName.startsWith("https:")){
            doc = doc.replace("src=\"/","src=\""+domainName);
            doc = doc.replace("src='/","src='"+domainName);
            doc = doc.replace("href=\"/","href=\""+domainName);
            doc = doc.replace("href='/","href='"+domainName);
        }
        return doc;
    }

    /**
     * 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
     * @param htmlStr
     * @return
     */
    public static String removeScriptTag(String htmlStr){
        String regex = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
        return removeTag(htmlStr,regex);
    }

    /**
     * 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
     * @param htmlStr
     * @return
     */
    public static String removeStyleTag(String htmlStr){
        String regex = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
        return removeTag(htmlStr,regex);
    }

    /**
     * 定义HTML标签的正则表达式
     * @param htmlStr
     * @return
     */
    public static String removeHtmlTag(String htmlStr){
        String regex = "<[^>]+>";
        return removeTag(htmlStr,regex);
    }

    /**
     * 定义一些特殊字符的正则表达式 如：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     * @param htmlStr
     * @return
     */
    public static String removeSpecialTag(String htmlStr){
        String regex = "\\&[a-zA-Z]{1,10};";
        return removeTag(htmlStr,regex);
    }

    private static String removeTag(String htmlStr,String regex){
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(htmlStr);
        htmlStr = matcher.replaceAll("");
        return htmlStr;
    }

}
